// pages/visitorCheckIn/visitorCheckIn.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    visitorName: '',
    visitorID: '',
    visitorTel: '',
    visitorReason: '',
    ownerTel: '',
    startTime: '请选择开始时间',
    endTime: '请选择结束时间',
    showStartTime: false,
    showEndTime: false,
    columns2: [],
    columns1: [],
  },
  formatTime: function (time, spliter) {
    var date = new Date(time);
    var year = date.getFullYear();
    var month = (date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1);
    var day = date.getDate() >= 10 ? date.getDate() : '0' + date.getDate();

    var str = "";
    return year + spliter + month + spliter + day
  },
  generateList: function (start, end) {
    let st = start.split('-');
    let et = end.split('-');
    let startTime = new Date(st[0], st[1] - 1, st[2]).getTime();
    let endTime = new Date(et[0], et[1] - 1, et[2]).getTime();
    let res = []
    for (let i = startTime; i <= endTime;) {
      res.push(this.formatTime(i, '-'));
      i += 24 * 60 * 60 * 1000;
    }
    return res
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let myDate = new Date();
    let date = util.formatTime(new Date(), "Y-M-D");
    var now = new Date()
    var seconds = 60 * 60 * 24 * 30 * 1000;
    var timestamp = now.getTime();
    var newDate = timestamp + seconds;
    var endDate = util.formatTime(new Date(newDate), "Y-M-D");
    console.log(date)
    console.log(endDate)
    this.setData({
      columns1: [{
          values: this.generateList(date, endDate),
          defaultIndex: 0,
        },
        // 第二列
        {
          values: Array.from({
            length: 24
          }, (item, index) => index + 1),
          defaultIndex: 0,
        },
        {
          values: Array.from({
            length: 60
          }, (item, index) => index + 1),
          defaultIndex: 0,
        },
      ],
      columns2: [{
          values: this.generateList(date, endDate),
          defaultIndex: 0,
        },
        // 第二列
        {
          values: Array.from({
            length: 24
          }, (item, index) => index + 1),
          defaultIndex: 0,
        },
        {
          values: Array.from({
            length: 60
          }, (item, index) => index + 1),
          defaultIndex: 0,
        },
      ]
    })
  },
  onStartConfirm: function (event) {
    console.log("确认开始时间")
    console.log(event.detail)
    this.setData({
      startTime: event.detail.value[0] + " " + event.detail.value[1] + ":" + event.detail.value[2],
      showStartTime: false
    })
  },
  onEndConfirm: function (event) {
    console.log("确认结束时间")
    console.log(event.detail)
    this.setData({
      endTime: event.detail.value[0] + " " + event.detail.value[1] + ":" + event.detail.value[2],
      showEndTime: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onStartTime: function () {
    this.setData({
      showStartTime: true
    })
  },
  onEndTime: function () {
    this.setData({
      showEndTime: true
    })
  },
  onCancelStart: function () {
    this.setData({
      showStartTime: false
    })
  },
  onCancelEnd: function () {
    this.setData({
      showEndTime: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onSubmit: function () {
    console.log("点击按钮事件")
    let _that = this
    if ((this.data.visitorName != "") && (this.data.visitorReason != "") && (this.data.visitorTel != "") && (this.data.visitorID != "")&& (this.data.ownerTel != "") && (this.data.startTime != "请选择开始时间") && (this.data.startTime != "请选择结束时间")) {
      console.log(wx.getStorageSync('openid')+":"+_that.data.visitorName)
      wx.request({
        url: 'http://localhost/Wx/AyMousComeSubmit',
        method:"post",
        header:{
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data:{
          openid:wx.getStorageSync('openid'),
          realname:_that.data.visitorName,
          idcard:_that.data.visitorID,
          amPhone:_that.data.visitorTel,
          story:_that.data.visitorReason,
          phone:_that.data.ownerTel,
          startDate:_that.data.startTime,
          endDate:_that.data.endTime
        },
        success(res){
          console.log(res)
          if (res.data.code == 400) {
            wx.showModal({
              title: '提示',
              content: '被访问者错误',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
          if (res.data.code == 200) {
            wx.showModal({
              title: '提示',
              content: '申请提交成功',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        }
      })
    } else {
      console.log("空")
      wx.showModal({
        title: '提示',
        content: '信息未填写完全',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onClickCheckinList:function(){
    wx.navigateTo({
      url: '../myApprovalLogs/myApprovalLogs',
    })
  },
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})